package com.ruoyi.generator.util;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.LikeTable;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Arrays;
import java.util.List;


public class CodeGenerator {
    // 参数列表
    private final static String author = "zxb";
    private final static String outDir = "/com/ruoyi/main/java";
    private final static Boolean fileOverride = true;
    private final static String url =
            "jdbc:mysql://auto_finance:W6w3XjrkeNwzcFZK@182.151.24.54:3306/auto_finance?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&verifyServerCertificate=false&useSSL=false";
    //    private final static String url =
//            "jdbc:mysql://root:efr123db@47.108.195.30:3306/frdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&verifyServerCertificate=false&useSSL=false";
    private final static String parent = "com.ruoyi.system";
    private final static LikeTable likeTable = new LikeTable("sys_company");
    private final static List<TableFill> tableFillList =
            Arrays.asList(new TableFill("created_time", FieldFill.INSERT), new TableFill("updated_time", FieldFill.INSERT_UPDATE));
//    private final static String versionFieldName = "updated_time";


    public static void main(String[] args) {


        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + outDir);
        gc.setAuthor(author);
        gc.setFileOverride(fileOverride);

        gc.setIdType(IdType.AUTO);
        gc.setOpen(false);
        gc.setSwagger2(true); //实体属性 Swagger2 注解
        gc.setDateType(DateType.TIME_PACK);

        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");

        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(parent);
        mpg.setPackageInfo(pc);


        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        templateConfig.setController(null);
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setChainModel(true);
//        strategy.setVersionFieldName(versionFieldName);
        strategy.setLikeTable(likeTable);
//        strategy.setExclude(excludeTable);
//        strategy.setExclude(excludeTable1);
        strategy.setTableFillList(tableFillList);
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}
